package service

import (
	"errors"
	"seat-service/initialization"
	"seat-service/model"
	"seat-service/model/dto"
)

type CarouselService struct {
}

func (*CarouselService) AddCarousel(carousel model.Carousel) error {
	return initialization.DB.Create(&carousel).Error
}
func (*CarouselService) DelCarousel(id uint) error {
	var carousel model.Carousel
	err := initialization.DB.Model(&model.Carousel{}).Where("id=?", id).First(&carousel).Error
	if err != nil {
		return err
	}
	if carousel.CarouselStatus == 1 {
		return errors.New("已发布状态无法删除")
	}
	err = initialization.DB.Delete(&model.Carousel{}, id).Error
	if err != nil {
		return err
	}
	return nil
}
func (*CarouselService) UpdateCarousel(carousel model.Carousel) error {
	var car model.Carousel
	err := initialization.DB.Model(&model.Carousel{}).Where("id=?", carousel.ID).First(&car).Error
	if err != nil {
		return err
	}
	if car.CarouselStatus == 1 {
		return errors.New("已发布状态无法修改")
	}
	err = initialization.DB.Updates(&carousel).Error
	if err != nil {
		return err
	}
	return nil
}
func (*CarouselService) QueryCarousel(page, pageSize int) (dto.CarouselReturnDto, error) {
	car := make([]model.Carousel, 0)

	//总数
	var record int64
	err := initialization.DB.Model(&model.Carousel{}).Count(&record).Error
	if err != nil {
		return dto.CarouselReturnDto{}, err
	}
	//页数
	pages := (record + int64(pageSize) - 1) / int64(pageSize)
	err = initialization.DB.Model(&model.Carousel{}).Offset((page - 1) * pageSize).Limit(pageSize).Find(&car).Error
	if err != nil {
		return dto.CarouselReturnDto{}, err
	}

	return dto.CarouselReturnDto{
		Record: record,
		Pages:  pages,
		Struct: car,
	}, nil

}
func (*CarouselService) QueryCarouselByTitle(page, pageSize int, title string) (dto.CarouselReturnDto, error) {
	car := make([]model.Carousel, 0)

	//总数
	var record int64
	err := initialization.DB.Model(&model.Carousel{}).Where("title Like ?", "%"+title+"%").Count(&record).Error
	if err != nil {
		return dto.CarouselReturnDto{}, err
	}
	//页数
	pages := (record + int64(pageSize) - 1) / int64(pageSize)

	err = initialization.DB.Model(&model.Carousel{}).Where("title Like ?", "%"+title+"%").Offset((page - 1) * pageSize).Limit(pageSize).Find(&car).Error
	if err != nil {
		return dto.CarouselReturnDto{}, err
	}

	return dto.CarouselReturnDto{
		Record: record,
		Pages:  pages,
		Struct: car,
	}, nil
}
func (*CarouselService) PublishStatusCarousel(id uint) error {
	var car model.Carousel
	err := initialization.DB.Model(&model.Carousel{}).Where("id=?", id).First(&car).Error
	if err != nil {
		return err
	}
	if car.CarouselStatus == 1 {
		car.CarouselStatus = 0
	} else {
		car.CarouselStatus = 1
	}
	err = initialization.DB.Model(&model.Carousel{}).Where("id=?", id).Update("carousel_status", car.CarouselStatus).Error
	if err != nil {
		return err
	}
	return nil
}

func (*CarouselService) GetByStatus(i int) (carousel []model.Carousel, err error) {
	err = initialization.DB.Where("carousel_status = ?", i).Find(&carousel).Error
	return
}
